package ru.javadevelop.vegetarian.model;

import ru.javadevelop.vegetarian.common.DB;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;

/**
 * Created with IntelliJ IDEA.
 * User: navff
 * Date: 09.03.13
 * Time: 11:58
 * To change this template use File | Settings | File Templates.
 */
public class Favorite {
    public String username;
    public int recipeId;
    public boolean isDeleted;

    public Favorite(String username, int recipeId, boolean isDeleted)
    {
        this.username = username;
        this.recipeId = recipeId;
        this.isDeleted = isDeleted;
    }

    //******************************************************************************************************************
    public int save() throws SQLException {

        boolean exists = this.recipeId != 0 && DB.INSTANCE.executeSql(new DB.Handler<Boolean>() {
            @Override
            public Boolean  execute(Statement stmt) throws SQLException {
                ResultSet resultSet = stmt.getResultSet();
                return resultSet.next();
            }
        }, "SELECT * FROM Favorite WHERE username=? AND recipeId=?", this.username, this.recipeId);
        // Если запись существовала, — обновляем её  (проверяется по id)
        // у вновь создаваемых записей id=0

        if  (exists)
        {
            DB.INSTANCE.executeSql("UPDATE Favorite SET isDeleted=? WHERE username = ? AND recipeId=?",
                    this.isDeleted, this.username, this.recipeId);
            return this.recipeId;
        }
        else
        {
            DB.INSTANCE.executeSql("INSERT INTO Favorite (username, recipeId, isDeleted) VALUES(?,?,?)",
                    this.username, this.recipeId, this.isDeleted);
            return this.recipeId;
        }


    }
    //******************************************************************************************************************
    public void delete() throws SQLException {

        DB.INSTANCE.executeSql("DELETE FROM Favorite WHERE username=? AND recipeId=?", this.username, this.recipeId);
    }
    //******************************************************************************************************************
    public static Favorite get(String username, int recipeId) {
        try
        {
            return DB.INSTANCE.executeSql(new DB.Handler<Favorite>() {
                @Override
                public Favorite execute(Statement stmt) throws SQLException {
                    ResultSet resultSet = stmt.getResultSet();
                    resultSet.next();
                    return  new Favorite(
                            resultSet.getString("username"),
                            resultSet.getInt("recipeId"),
                            resultSet.getBoolean("isDeleted"));
                }
            }, "SELECT * FROM Favorite WHERE username=? AND recipeId=?", username, recipeId);
        }
        catch (Exception e)
        {
            return null;
        }

    }
    //******************************************************************************************************************
    public static ArrayList<Favorite> getAllForUser(String username)
    {
        return DB.INSTANCE.executeSql(new DB.Handler<ArrayList<Favorite>>() {
            @Override
            public ArrayList<Favorite> execute(Statement stmt) throws SQLException {
                ArrayList<Favorite> result = new ArrayList<Favorite>();
                ResultSet resultSet = stmt.getResultSet();
                while (resultSet.next())
                {
                    result.add(new Favorite(
                            resultSet.getString("username"),
                            resultSet.getInt("recipeId"),
                            resultSet.getBoolean("isDeleted"))
                    );
                }
                return result;

            }
        }, "SELECT * FROM Favorite WHERE username=? AND isdeleted=false", username);
    }
    //******************************************************************************************************************
    public static  void deleteAll() {
        DB.INSTANCE.executeSql("DELETE FROM Favorite");
    }
    //******************************************************************************************************************
    public static ArrayList<Favorite> getAllDeletedForUser(String username)
    {
        return DB.INSTANCE.executeSql(new DB.Handler<ArrayList<Favorite>>() {
            @Override
            public ArrayList<Favorite> execute(Statement stmt) throws SQLException {
                ArrayList<Favorite> result = new ArrayList<Favorite>();
                ResultSet resultSet = stmt.getResultSet();
                while (resultSet.next())
                {
                    result.add(new Favorite(
                            resultSet.getString("username"),
                            resultSet.getInt("recipeId"),
                            resultSet.getBoolean("isDeleted"))
                    );
                }
                return result;

            }
        }, "SELECT * FROM Favorite WHERE username=? AND isDeleted=true", username);
    }
    //******************************************************************************************************************

}
